package com.yanqu.road.dao.impl.cross.party;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.invitation.CrossPartyRelationData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossPartyRelationDaoImpl extends TempDataObjectDao<CrossPartyRelationData> {
    public static String getCrossPartyRelationDataSql = "select * from `t_c_banquet_relation` where `lastJoinTime` > ?";
    public List<CrossPartyRelationData> getCrossPartyRelationData(long lastJoinTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossPartyRelationData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossPartyRelationDataSql);
            int i = 1;
            call.setLong(i++, lastJoinTime);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateCrossPartyRelationDataSql = "update `t_c_banquet_relation` set joinTimes1=?,joinTimes2=?,lastJoinTime=? where userId1=? and userId2=? and partyType=? ";
    @Override
    protected boolean updateData(CrossPartyRelationData crossPartyRelationData) {
        crossPartyRelationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getJoinTimes1()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getJoinTimes2()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getLastJoinTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getUserId1()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getUserId2()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getPartyType()));
        if (getDb().executeNoneQuery(updateCrossPartyRelationDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossPartyRelationDataSql = "replace into `t_c_banquet_relation`(userId1,joinTimes1,userId2,joinTimes2,partyType,lastJoinTime) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossPartyRelationData crossPartyRelationData) {
        crossPartyRelationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getUserId1()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getJoinTimes1()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getUserId2()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getJoinTimes2()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getPartyType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getLastJoinTime()));
        if (getDb().executeNoneQuery(insertCrossPartyRelationDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossPartyRelationData parseData(ResultSet rs) throws SQLException {
        CrossPartyRelationData crossPartyRelationData = new CrossPartyRelationData();
        crossPartyRelationData.setUserId1(rs.getLong("userId1"));
        crossPartyRelationData.setJoinTimes1(rs.getInt("joinTimes1"));
        crossPartyRelationData.setUserId2(rs.getLong("userId2"));
        crossPartyRelationData.setJoinTimes2(rs.getInt("joinTimes2"));
        crossPartyRelationData.setPartyType(rs.getInt("partyType"));
        crossPartyRelationData.setLastJoinTime(rs.getLong("lastJoinTime"));

        return crossPartyRelationData;
    }
}
